package Dao;

import entity.User;
import util.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends BasDao implements UserDao {

    public UserDaoImpl(Connection conn) {
        super(conn);
    }

    @Override
    public User login(String username, String password) throws SQLException {
        String sql = "SELECT id,username,email,`password`,`name`,phone,address,isadmin,isvalidate FROM `user` " +
                " WHERE username = ? and `password` = ?";
        User user = null;
        ResultSet rs = null;
        rs = executeQuery(sql,username,password);
        if (rs.next()){
            user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setEmail(rs.getString("email"));
            user.setPassword(rs.getString("password"));
            user.setName(rs.getString("name"));
            user.setPhone(rs.getString("phone"));
            user.setAddress(rs.getString("address"));
            user.setIsadmin(rs.getInt("isadmin"));
            user.setIsvalidate(rs.getInt("isvalidate"));
        }
        return user;
    }

    @Override
    public int register(User user) throws SQLException {
        String sql = "INSERT INTO `cake-shop`.`user`(`username`, `email`, `password`, `name`, " +
                "`phone`, `address`, `isadmin`, `isvalidate`) VALUES (?, ?, ?, ?, ?, ?, 0, 0);";
        int i = executeUpdate(sql,user);
        return i;
    }



    @Override
    public User phones(String phone) throws SQLException {
        String sql = "SELECT id,username,email,`password`,`name`,phone,address,isadmin,isvalidate FROM `user` WHERE phone = ?";
        User user = null;
        ResultSet rs = null;
        rs = executeQuery(sql,phone);
        if (rs.next()){
            user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setEmail(rs.getString("email"));
            user.setPassword(rs.getString("password"));
            user.setName(rs.getString("name"));
            user.setPhone(rs.getString("phone"));
            user.setAddress(rs.getString("address"));
            user.setIsadmin(rs.getInt("isadmin"));
            user.setIsvalidate(rs.getInt("isvalidate"));
        }
        return user;
    }

    @Override
    public User emails(String email) throws SQLException {
        String sql = "SELECT id,username,email,`password`,`name`,phone,address,isadmin,isvalidate FROM `user` WHERE email = ?";
        User user = null;
        ResultSet rs = null;
        rs = executeQuery(sql,email);
        if (rs.next()){
            user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setEmail(rs.getString("email"));
            user.setPassword(rs.getString("password"));
            user.setName(rs.getString("name"));
            user.setPhone(rs.getString("phone"));
            user.setAddress(rs.getString("address"));
            user.setIsadmin(rs.getInt("isadmin"));
            user.setIsvalidate(rs.getInt("isvalidate"));
        }
        return user;
    }

    @Override
    public User usernames(String username) throws SQLException {
        String sql = "SELECT id,username,email,`password`,`name`,phone,address,isadmin,isvalidate FROM `user` WHERE username = ?";
        User user = null;
        ResultSet rs = null;
        rs = executeQuery(sql,username);
        if (rs.next()){
            user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setEmail(rs.getString("email"));
            user.setPassword(rs.getString("password"));
            user.setName(rs.getString("name"));
            user.setPhone(rs.getString("phone"));
            user.setAddress(rs.getString("address"));
            user.setIsadmin(rs.getInt("isadmin"));
            user.setIsvalidate(rs.getInt("isvalidate"));
        }
        return user;
    }

    @Override
    public User getMessage(String name) {
        String sql = "select * from user where username = ?";
        User user = null;
        ResultSet rs = null;
        rs = executeQuery(sql,"%"+name+"%");
        try {
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setEmail(rs.getString("email"));
                user.setPassword(rs.getString("password"));
                user.setName(rs.getString("name"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setIsadmin(rs.getInt("isadmin"));
                user.setIsvalidate(rs.getInt("isvalidate"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int updateUser(User user) {
        ResultSet rs = null;
        int num=0;
        PreparedStatement pstmt = null;
        String sql = "UPDATE `cake-shop`.`user` SET `name` = ?, `phone` = ?, `address` = ? WHERE `id` =?;";
        try{
            num =  super.executeUpdate(sql,user.getName(),user.getPhone(),user.getAddress(),user.getId());
        } finally {
            DatabaseUtil.closeAll(null, pstmt, rs);
        }
        return num;
    }

    @Override
    public int updatePwd(User user) {
        ResultSet rs = null;
        int num=0;
        PreparedStatement pstmt = null;
        String sql = " UPDATE `cake-shop`.`user` SET `password`=?  WHERE `id` = ?;";
        try{
            num =  super.executeUpdate(sql,user.getPassword(),user.getId());
        } finally {
            DatabaseUtil.closeAll(null, pstmt, rs);
        }
        return num;
    }

    @Override
    public int wogout(int id) {
        String sql = "DELETE FROM `user` WHERE id = ?;";
        int i = executeUpdate(sql,id);
        return i;
    }
}
